TARGET = tools
OBJDIR = build

RELEASE_PLATFORM ?= arm

ifeq ($(RELEASE_PLATFORM), arm)
CROSS_COMPILE := aarch64-buildroot-linux-gnu-
else
CROSS_COMPILE := 
endif

CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)g++
STRIP=$(CROSS_COMPILE)strip

CFLAGS += -g -Wall -O3
CXXFLAGS += -g -std=c++11

INCLUDES += -I./utils
INCLUDES += -I./i2c 
INCLUDES += -I./net 
INCLUDES += -I./uart 
INCLUDES += -I./can
INCLUDES += -I./shell 

ifeq ($(RELEASE_PLATFORM), arm)
INCLUDES += -I./3rd/readline/include
LIBS += -L./3rd/readline/lib
endif

MAIN_OBJS += main.o dispatch.o 
MAIN_OBJS += i2c/i2c.o
MAIN_OBJS += net/network.o
MAIN_OBJS += uart/uart.o
MAIN_OBJS += can/can.o
MAIN_OBJS += shell/shell.o
MAIN_OBJS += utils/lock.o utils/log.o utils/wait.o 

LDFLAGS += -g
LDFLAGS += $(LIBS)
LDFLAGS += -lreadline
LDFLAGS += -lpthread

VPATH += utils
VPATH += i2c
VPATH += net
VPATH += uart
VPATH += can
VPATH += shell

OBJS = $(patsubst %, $(OBJDIR)/%, $(notdir $(MAIN_OBJS)))
                                                                                                                        
all:dir_create $(TARGET)

$(TARGET):$(OBJS)
	$(LD) $(OBJS) -o $(TARGET)_g $(LDFLAGS) 
	$(STRIP) --strip-unneeded -o $(TARGET) $(TARGET)_g

define CREATE_DIR
	if [ ! -d $(1) ];\
	 	then\
    	mkdir -p $(1);\
	fi	
endef

dir_create:  
	@$(call CREATE_DIR,$(OBJDIR))

clean:
	rm -rf $(OBJDIR)/*
	rm -f $(TARGET)_g $(TARGET)

$(OBJDIR)/%.o:%.S
	@echo "CC $<"
	@$(CC) $(INCLUDES) $(CFLAGS) -o $@ -c $< 
$(OBJDIR)/%.o:%.c
	@echo "CC $<"
	@$(CC) $(INCLUDES) $(CFLAGS) -o $@ -c $< 
$(OBJDIR)/%.o:%.cpp
	@echo "CXX $<"
	@$(CXX) $(INCLUDES) $(CXXFLAGS) -o $@ -c $< 


